package com.longrise.common.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.common.R;
import com.longrise.common.utils.ActivityUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.GenericUtil;
import com.longrise.common.utils.NetUtil;


/**
 * Created by callxkj on 2017/10/17.
 * mvp的base Activity类 增加对网络错误的处理 后期的页面使用这个
 */

public abstract class BaseActivity2<P extends BasePresenter> extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "BaseActivity2";
    public P mPresenter;
    public Context mContext;
    public RxManager mRxmanager;
    private View mRootView;
    public Toolbar mToolbar;
    private View mInclude_NetErrorPage; //网络加载失败显示的页面
    private View mInclude_content;
    private View mInclude_loading_normal;
    private View mInclude_loading_transparent;
    private View mInclude_title;
    private TextView mTvToolbarRighttv;
    private RelativeLayout mRlToolbarRigthtv;
    private RelativeLayout mRlToolbarrightiv;
    private ImageView mIvToolbarRightiv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeSetContentView();
        assembleView(savedInstanceState);
        initBaseView();
        ActivityUtil.addActivity(this);
        mContext = this;
        mPresenter = GenericUtil.getT(this, 0);
        mRxmanager = new RxManager();
        if (mPresenter != null && this instanceof BaseView) {
            mPresenter.mContext = this;
            mPresenter.attachV(this);
            mPresenter.setRxManager(mRxmanager);
        }
        this.initView();
        if (mPresenter != null && this instanceof BaseView) {
            mPresenter.init();
        }
    }

    /**
     * 组装view
     */
    public void assembleView(@Nullable Bundle savedInstanceState) {
        int contentViewId = getContentViewId(savedInstanceState);
        View childView = View.inflate(this, contentViewId, null);

        mRootView = View.inflate(this, R.layout.activity_base, null);
        FrameLayout mContentView = (FrameLayout) mRootView.findViewById(R.id.base_content);
        mContentView.addView(childView);

        setContentView(mRootView);
    }

    /**
     * 初始化baseview
     */
    public void initBaseView() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mTvToolbarRighttv = (TextView) findViewById(R.id.iv_toolbar_right_tv);
        mRlToolbarRigthtv = (RelativeLayout) findViewById(R.id.bb_toolbar_right_rl_tv);
        mInclude_title = findViewById(R.id.base_title);
        mInclude_content = findViewById(R.id.base_content);
        mInclude_NetErrorPage = findViewById(R.id.base_net_error);
        mInclude_loading_normal = findViewById(R.id.base_loading_normal);
        mInclude_loading_transparent = findViewById(R.id.base_loading_transparent);
        mInclude_NetErrorPage.setOnClickListener(this);
        mRlToolbarrightiv = (RelativeLayout) findViewById(R.id.bb_toolbar_right_rl);
        mIvToolbarRightiv = (ImageView) findViewById(R.id.iv_toolbar_share);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onToolbarBackClick();
            }
        });


    }


    /*********************
     * 子类实现
     *****************************/
    // 获取布局文件
    public abstract int getContentViewId(@Nullable Bundle savedInstanceState);

    //初始化view
    public abstract void initView();

    //导航栏上面的返回键被点击
    public abstract void onToolbarBackClick();

    //重新加载, 如果用了errorpage则需要复写这个方法
    public void reload() {
    }

    //如果页面标题有右面的text的话 就要复写这个
    public void onToolbarRightTvClick() {

    }

    //如果页面标题有右面的imageview的话 就要复写这个
    public void onToolbarRightIvClick() {

    }
    /*******************************************************/

    /**
     * 设置contentView之前的一些配置
     */
    public void beforeSetContentView() {
        //初始化主题
        initTheme();
        //没有title
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        //默认竖屏
        orientationToPortrait();
        // 设置状态栏颜色
        setetStatusBarColor();
    }

    public void initTheme() {
        setTheme(R.style.AppCompatThem);
    }

    public void setetStatusBarColor() {
    }

    /**
     * 竖屏
     */
    protected void orientationToPortrait() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /**
     * 横屏
     */
    protected void orientationToLandscape() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    /**
     * 全屏
     */
    protected void screenToFull() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 状态显示状态
     * View.SYSTEM_UI_FLAG_VISIBLE 可见状态
     * View.INVISIBLE 不可见状态，Activity会伸展全屏显示
     * View.SYSTEM_UI_FLAG_FULLSCREEN Activity全屏显示，且状态栏被隐藏覆盖掉
     * View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN Activity全屏显示，但状态栏不会被隐藏覆盖，状态栏依然可见，Activity顶端布局部分会被状态遮住。
     */
    protected void setStatusBarVisiable(int visiable) {
        getWindow().getDecorView().setSystemUiVisibility(visiable);
    }

    /**
     * 设置是否显示toolbar 例如主页就不需要toolbar
     *
     * @param visible true: 显示toolbar
     */
    public void setToolbarVisible(boolean visible) {
        if (visible) {
            mInclude_title.setVisibility(View.VISIBLE);
        } else {
            mInclude_title.setVisibility(View.GONE);
        }
    }

    /**
     * 一般的 设置title的方法
     *
     * @param title 标题
     */
    public void setToolbarTitle(String title) {
        mToolbar.setTitle(title);
    }

    /**
     * 设置右边标题
     * 需要复写 onToolbarRightTvClick() 这个方法
     *
     * @param title 标题
     */
    public void setToolbarRightTextTitle(String title) {
        mRlToolbarRigthtv.setVisibility(View.VISIBLE);
        mTvToolbarRighttv.setText(title);
        mRlToolbarRigthtv.setOnClickListener(this);
    }

    /**
     * 显示toolbar右边面的iv
     */
    public void setToolbarRightIvTitle(int id) {
        mRlToolbarrightiv.setVisibility(View.VISIBLE);
        mIvToolbarRightiv.setVisibility(View.VISIBLE);
        mIvToolbarRightiv.setImageResource(id);
        mRlToolbarrightiv.setOnClickListener(this);
    }

    /**
     * 隐藏toolbar右边的iv
     */
    public void hideToolbarRightIvTitle() {
        mRlToolbarrightiv.setVisibility(View.GONE);
        mIvToolbarRightiv.setVisibility(View.GONE);
    }

    /**
     * 设置toolbar上面的返回按钮 例如叉叉 箭头
     */
    public void setToolbarBackIcon(@DrawableRes int id){
        mToolbar.setNavigationIcon(id);
    }

    /**
     * activity 跳转
     *
     * @param cls 需要跳转的activity
     */
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * @param cls    需要跳转的activity
     * @param bundle bundle传输数据
     */
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 没有网络了就显示这个页面
     */
    public void showNonNetPage() {
        dismissLoadingDialog();
        showPage(true, false, false, false);
    }

    /**
     * 显示内容页面
     */
    public void showContentPage() {
        dismissLoadingDialog();
        showPage(false, true, false, false);
    }

    /**
     * webview一般的加载页面
     */
    public void showNormalLoadingPage() {
        dismissLoadingDialog();
        showPage(false, false, true, false);
    }

    /**
     * 背景时透明的加载页面
     */
    public void showTransparentLoadingPage() {
        dismissLoadingDialog();
        showPage(false, false, false, true);
    }

    /**
     * 显示网络加载dialog
     */
    public void showLoadingDialog() {
        DZZWTools.showWaitDialog(this);
    }

    /**
     * 隐藏网络加载dialog
     */
    public void dismissLoadingDialog() {
        DZZWTools.dismissDialog(null);
    }

    /**
     * 弹吐司
     */
    public void showToast(String msg) {
        DZZWTools.showToast(this, msg, Toast.LENGTH_SHORT);
    }

    public void showSnakbar(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Snackbar.make(getWindow().getDecorView(), msg, Snackbar.LENGTH_SHORT).show();
        }
    }

    /**
     * 检查网络状况
     */
    public boolean checkNetWorkEnable() {
        if (NetUtil.checkNetEnable()) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtil.removeActivity(this);
        if (mPresenter != null) {
            mPresenter.detachV();
        }
        if (mRxmanager != null) {
            mRxmanager.clear();
        }
    }

    //显示隐藏页面
    private void showPage(boolean errorpage, boolean content, boolean loading_normal, boolean loading_trasn) {
        if (errorpage) {
            mInclude_NetErrorPage.setVisibility(View.VISIBLE);
        } else {
            mInclude_NetErrorPage.setVisibility(View.GONE);
        }
        if (content) {
            mInclude_content.setVisibility(View.VISIBLE);
        } else {
            mInclude_content.setVisibility(View.GONE);
        }
        if (loading_normal) {
            mInclude_loading_normal.setVisibility(View.VISIBLE);
        } else {
            mInclude_loading_normal.setVisibility(View.GONE);
        }
        if (loading_trasn) {
            mInclude_loading_transparent.setVisibility(View.VISIBLE);
        } else {
            mInclude_loading_transparent.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.base_net_error == id) {
            reload(); //重新加载
        } else if (R.id.bb_toolbar_right_rl_tv == id) {
            onToolbarRightTvClick(); //点击toolbar右边的标题
        } else if (R.id.bb_toolbar_right_rl == id) {
            onToolbarRightIvClick(); //点击toolbar右边的iv
        }
    }
}
